"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogFileUtil = void 0;
const DateUtil_1 = require("./DateUtil");
const Logger_1 = require("./Logger");
const fs = require("fs");
const path = require("path");
class LogFileUtil {
    static mkdirsSync(dirname) {
        if (fs.existsSync(dirname)) {
            return true;
        }
        else {
            if (this.mkdirsSync(path.dirname(dirname))) {
                fs.mkdirSync(dirname);
                return true;
            }
        }
    }
    static init() {
        if (this.canLog && !this.isInited) {
            this.isInited = true;
            this.Log_Path = path.resolve() + "/logs";
            console.log("Log_Path=====", this.Log_Path);
            this.mkdirsSync(this.Log_Path);
        }
    }
    static writeLog(logStr, folderName, fileName) {
        if (!fs.existsSync(folderName)) {
            fs.mkdirSync(folderName);
        }
        fileName = Logger_1.Logger.tag + "-" + fileName;
        let filedir = path.join(folderName, fileName);
        let buf = Buffer.from("");
        for (let i = 0; i < Number.MAX_VALUE; i++) {
            let tempFileDir = filedir;
            tempFileDir += i;
            if (fs.existsSync(tempFileDir)) {
                let stat = fs.statSync(tempFileDir);
                if (stat.size < 1048576) {
                    buf = fs.readFileSync(tempFileDir);
                    filedir = tempFileDir;
                    break;
                }
            }
            else {
                filedir = tempFileDir;
                break;
            }
        }
        let tempBuf = Buffer.from(logStr + "\n");
        let newBuff;
        if (buf != null) {
            newBuff = Buffer.concat([buf, tempBuf], tempBuf.length + buf.length);
        }
        else {
            newBuff = tempBuf;
        }
        // console.log("filedir===", filedir)
        fs.writeFileSync(filedir, newBuff);
    }
    static debug(str) {
        if (this.canLog) {
            this.init();
            let fileName = this.Log_File_Name_List[0 /* LOG_LEVEL_TYPES.DEBUG */] + "-" + DateUtil_1.default.formatDateStr3(DateUtil_1.default.now()) + this.Ext_Name;
            this.writeLog(str, this.Log_Path, fileName);
        }
    }
    static log(str) {
        if (this.canLog) {
            this.init();
            let fileName = this.Log_File_Name_List[1 /* LOG_LEVEL_TYPES.LOG */] + "-" + DateUtil_1.default.formatDateStr3(DateUtil_1.default.now()) + this.Ext_Name;
            this.writeLog(str, this.Log_Path, fileName);
        }
    }
    static info(str) {
        if (this.canLog) {
            this.init();
            let fileName = this.Log_File_Name_List[2 /* LOG_LEVEL_TYPES.INFO */] + "-" + DateUtil_1.default.formatDateStr3(DateUtil_1.default.now()) + this.Ext_Name;
            this.writeLog(str, this.Log_Path, fileName);
        }
    }
    static warn(str) {
        if (this.canLog) {
            this.init();
            let fileName = this.Log_File_Name_List[3 /* LOG_LEVEL_TYPES.WARN */] + "-" + DateUtil_1.default.formatDateStr3(DateUtil_1.default.now()) + this.Ext_Name;
            this.writeLog(str, this.Log_Path, fileName);
        }
    }
    static error(str) {
        if (this.canLog) {
            this.init();
            let fileName = this.Log_File_Name_List[4 /* LOG_LEVEL_TYPES.ERROR */] + "-" + DateUtil_1.default.formatDateStr3(DateUtil_1.default.now()) + this.Ext_Name;
            this.writeLog(str, this.Log_Path, fileName);
        }
    }
}
exports.LogFileUtil = LogFileUtil;
LogFileUtil.Log_File_Name_List = [
    "debug",
    "log",
    "info",
    "warn",
    "error",
    "clientLog"
];
LogFileUtil.Ext_Name = ".log";
LogFileUtil.canLog = false;
LogFileUtil.isInited = false;
